package org.mat.ipaas.ucenter.bff.admin.modules.system;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysRoleRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysRoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @Author scott
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/sys/role")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleRestAPI sysRoleRestAPI;

    /**
     * 分页列表查询 【系统角色，不做租户隔离】
     *
     * @param sysRoleDTO
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@RequiresPermissions("system:role:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MatApiResponse<Page<SysRoleDTO>> queryPageList(@SpringQueryMap SysRoleDTO sysRoleDTO,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<Page<SysRoleDTO>> matApiResponse = sysRoleRestAPI.queryPageList(sysRoleDTO, pageNo, pageSize);
        return matApiResponse;
    }

    /**
     * 分页列表查询【租户角色，做租户隔离】
     *
     * @param sysRoleDTO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/listByTenant", method = RequestMethod.GET)
    public MatApiResponse<Page<SysRoleDTO>> listByTenant(SysRoleDTO sysRoleDTO,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<Page<SysRoleDTO>> matApiResponse = sysRoleRestAPI.listByTenant(sysRoleDTO, pageNo, pageSize);
        return matApiResponse;
    }

    /**
     * 添加
     *
     * @param sysRoleDTO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@RequiresPermissions("system:role:add")
    public MatApiResponse<SysRoleDTO> add(@RequestBody SysRoleDTO sysRoleDTO) {
        MatApiResponse<SysRoleDTO> matApiResponse = sysRoleRestAPI.add(sysRoleDTO);
        return matApiResponse;
    }

    /**
     * 编辑
     *
     * @param sysRoleDTO
     * @return
     */
    //@RequiresPermissions("system:role:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<SysRoleDTO> edit(@RequestBody SysRoleDTO sysRoleDTO) {
        MatApiResponse<SysRoleDTO> matApiResponse = sysRoleRestAPI.edit(sysRoleDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    //@RequiresPermissions("system:role:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public MatApiResponse<?> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = sysRoleRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    //@RequiresPermissions("system:role:deleteBatch")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public MatApiResponse<SysRoleDTO> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<SysRoleDTO> matApiResponse = sysRoleRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public MatApiResponse<SysRoleDTO> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<SysRoleDTO> matApiResponse = sysRoleRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 查询全部角色（参与租户隔离）
     *
     * @return
     */
    @RequestMapping(value = "/queryall", method = RequestMethod.GET)
    public MatApiResponse<List<SysRoleDTO>> queryall() {
        MatApiResponse<List<SysRoleDTO>> matApiResponse = sysRoleRestAPI.queryall();
        return matApiResponse;
    }

    /**
     * 查询全部系统角色（不做租户隔离）
     *
     * @return
     */
    //@RequiresPermissions("system:role:queryallNoByTenant")
    @RequestMapping(value = "/queryallNoByTenant", method = RequestMethod.GET)
    public MatApiResponse<List<SysRoleDTO>> queryallNoByTenant() {
        MatApiResponse<List<SysRoleDTO>> matApiResponse = sysRoleRestAPI.queryallNoByTenant();
        return matApiResponse;
    }

    /**
     * 校验角色编码唯一
     */
    @RequestMapping(value = "/checkRoleCode", method = RequestMethod.GET)
    public MatApiResponse<Boolean> checkUsername(String id, String roleCode) {
        MatApiResponse<Boolean> matApiResponse = sysRoleRestAPI.checkUsername(id, roleCode);
        return matApiResponse;
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysRoleDTO sysRoleDTO) {
        ModelAndView modelAndView = sysRoleRestAPI.exportXls(sysRoleDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() {
        MatApiResponse<?> matApiResponse = sysRoleRestAPI.importExcel();
        return matApiResponse;
    }

    /**
     * 查询数据规则数据
     */
    @GetMapping(value = "/datarule/{permissionId}/{roleId}")
    public MatApiResponse<?> loadDatarule(@PathVariable("permissionId") String permissionId, @PathVariable("roleId") String roleId) {
        MatApiResponse<?> matApiResponse = sysRoleRestAPI.loadDatarule(permissionId, roleId);
        return matApiResponse;
    }

    /**
     * 保存数据规则至角色菜单关联表
     */
    @PostMapping(value = "/datarule")
    public MatApiResponse<?> saveDatarule(@RequestBody JSONObject jsonObject) {
        MatApiResponse<?> matApiResponse = sysRoleRestAPI.saveDatarule(jsonObject);
        return matApiResponse;
    }


    /**
     * 用户角色授权功能，查询菜单权限树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public MatApiResponse<Map<String, Object>> queryTreeList(HttpServletRequest request) {
        MatApiResponse<Map<String, Object>> matApiResponse = sysRoleRestAPI.queryTreeList(request);
        return matApiResponse;
    }

}
